package QueueDemo;

public class ArrayQueue {
    private int[] queue;
    private int front;  // 队头指针
    private int rear;   // 队尾指针
    private int capacity; // 队列容量

    // 构造函数，初始化队列容量
    public ArrayQueue(int capacity) {
        this.capacity = capacity;
        queue = new int[capacity];
        front = -1;
        rear = -1;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return front == -1;
    }

    // 判断队列是否已满
    public boolean isFull() {
        return rear == capacity - 1;
    }

    // 入队列操作
    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("队列已满，无法入队");
            return;
        }
        if (isEmpty()) {
            front = 0;
        }
        rear++;
        queue[rear] = value;
        System.out.println("入队列元素值: " + value);
    }

    // 出队列操作
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("队列已空，无法出队");
            return -1;
        }
        int removed = queue[front];
        if (front == rear) {
            front = -1;
            rear = -1;
        } else {
            front++;
        }
        System.out.println("出队列元素值: " + removed);
        return removed;
    }

    // 显示队列当前元素
    public void display() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        System.out.print("队列当前元素值: ");
        for (int i = front; i <= rear; i++) {
            System.out.print(queue[i] + " ");
        }
        System.out.println();
    }

    // 获取队列首元素
    public int peek() {
        if (isEmpty()) {
            System.out.println("队列为空，无首元素");
            return -1;
        }
        return queue[front];
    }
}
